//
//  KafkaRefresh+Rx.swift
//  SwiftHub
//
//  Created by Khoren Markosyan on 7/24/18.
//  Copyright © 2018 Khoren Markosyan. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift
import KafkaRefresh
import RxTheme

extension Reactive where Base: KafkaRefreshControl {

    public var isAnimating: Binder<Bool> {
        return Binder(self.base) { refreshControl, active in
            if active {
//                refreshControl.beginRefreshing()
            } else {
                refreshControl.endRefreshing()
            }
        }
    }
    
//    var themeColor: Binder<UIColor> {
//        return Binder(self.base) { refreshControl, color in
//            refreshControl.themeColor = color
//        }
//    }
}

extension Reactive where Base: KafkaRefreshDefaults {

    var themeColor: Binder<UIColor?> {
        return Binder(self.base) { view, attr in
            view.themeColor = attr
        }
    }
}


extension ThemeProxy where Base: KafkaRefreshDefaults {
    
    var themeColor: ThemeAttribute<UIColor> {
        get { fatalError("set only") }
        set {
            base.themeColor = newValue.value
            let disposable = newValue.stream
                .take(until: base.rx.deallocating)
                .observe(on: MainScheduler.instance)
                .bind(to: base.rx.themeColor)
            hold(disposable, for: "themeColor")
        }
    }
    
}
